package com.gardenguell.salad.ui.activity.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppConstant;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.app.HandlerCenter;
import com.gardenguell.salad.data.volley.OnErrorResponse;
import com.gardenguell.salad.ui.activity.LoginActivity;
import com.gardenguell.salad.util.CommonLog;
import com.gardenguell.salad.util.SharedPreferenceUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by absurd on 2015/11/14.
 */
public class BaseActivity extends AppCompatActivity implements View.OnClickListener, OnErrorResponse{

    protected CommonLog log = CommonLog.getInstance();

    private ProgressDialog mProgressDialog;

    protected SharedPreferenceUtil prefUtil = SharedPreferenceUtil.getInstance();

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case HandlerCenter.Message.DISMISS_PROGRESS:
                    dismissProgressDialog();
                    break;
                case HandlerCenter.Message.SHOW_PROGRESS:
                    String tip = (String)msg.obj;
                    showProgress(tip, null);
                    break;
                case HandlerCenter.Message.WRONG_TOKEN:
                    if(!(BaseActivity.this instanceof LoginActivity)){
                        startActivity(new Intent(BaseActivity.this, LoginActivity.class));
                        AppContext.getContext().finishAllActivity();
                    }
                    break;
                case HandlerCenter.Message.SHOW_TOAST:
                    String toastMessage = (String)msg.obj;
                    Toast.makeText(BaseActivity.this, toastMessage, Toast.LENGTH_LONG).show();

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HandlerCenter.getInstance().setHandler(HandlerCenter.Flag.BASE_ACTIVITY, mHandler);
        AppContext.getContext().addActivity(this);
    }



    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.iv_actionbar_left:
                Toast.makeText(this, "left clicked!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_actionbar_right:
                Toast.makeText(this, "right clicked!", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }


    protected String getUsername(){
        return prefUtil.get(AppConstant.PREF_TOKEN.AUTH_USERNAME, "");
    }

    protected String getToken(){
        return prefUtil.get(AppConstant.PREF_TOKEN.AUTH_TOKEN, "");
    }



    /*
     * Progress Dialog utils
     */

    protected ProgressDialog showProgressDialog(String title, String message) {
        return ProgressDialog.show(this, title, message);
    }

    protected Dialog showProgress(String tip, DialogInterface.OnCancelListener listener) {
        dismissProgressDialog();
        mProgressDialog = showProgressDialog(null,tip);
        if (listener != null) {
            mProgressDialog.setCancelable(true);
            mProgressDialog.setOnCancelListener(listener);
        }
        return mProgressDialog;
    }


    public void dismissProgressDialog() {
        try {
            if (this.mProgressDialog != null) {
                this.mProgressDialog.dismiss();
            }
        } catch (final IllegalArgumentException e) {
            log.e(e);
        } catch (final Exception e) {
            log.e(e);
        } finally {
            this.mProgressDialog = null;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName());
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName());
        MobclickAgent.onPause(this);
    }


    @Override
    public void onError(int errorCode) {

    }
}
